/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.sql.SQLException;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import negocio.FacadeBusiness;
import negocio.ResultCalcDataBase;
import negocio.ResultCalcTable;

/**
 *
 * @author Angelo
 */
public class CtrlGui {

    private static CtrlGui ctrl = null;
    private FacadeBusiness f;
    private ResultCalcDataBase resultCalcDataBase;

    private CtrlGui() 
    {
        f = new FacadeBusiness();
    }

    public static CtrlGui getInstance() {
        if (ctrl == null) {
            ctrl = new CtrlGui();
        }
        return ctrl;
    }

    public void connect(String usuario, String senha, String host, int porta, String sid) throws SQLException, ClassNotFoundException {
        f.connect(usuario, senha, host, porta, sid);
    }

    public ComboBoxModel getOwners() throws SQLException {
        DefaultComboBoxModel m = new DefaultComboBoxModel();
        for (String s : f.getOwners()) 
        {
            m.addElement(s);
        }
        return m;
    }
    
    public boolean createStructure() throws SQLException
    {
        return f.createStructure();
    }
    
    
    public boolean reCreateStructure(String owner) throws SQLException
    {
        return f.reCreateStructure(owner);
    }

    public boolean hasStructure(String owner) throws SQLException
    {
        return f.hasStructure(owner);
    }
    
    public void setOwner(String owner) throws SQLException 
    {
        f.setOwner(owner);
    }

    public TableModel getTables() throws Exception {
        return f.getTables();
    }

    public TableModel getColumns(String table_name) {
        return f.getColumns(table_name);
    }

    public void updateTable(String table, int records, double growth, int retention) throws Exception {
        f.updateTable(table, records, growth, retention);
    }

    public void updateColumn(String table, String column, Double avgSize, Double percNull) throws Exception {
        f.updateColumn(table, column, avgSize, percNull);
    }
    
    public void writeData() throws SQLException{
        f.writeData();
    }
    
    public void restultCalc() throws SQLException
    {
        resultCalcDataBase = f.restultCalc();
    }
    
    public TableModel getResultDataBase() throws Exception 
    {
        DefaultTableModel m = new DefaultTableModel();
        String[] c = {"Liquido","Bruto", "Liquido Inicial", "Bruto Inicial", "Liquido Proximos anos", "Bruto Proximos anos"};
        m.setColumnIdentifiers(c);
        
        Object[] d = new Object[6];
        
        d[0] = standardizeReturn(resultCalcDataBase.getLIQUIDO());
        d[1] = standardizeReturn(resultCalcDataBase.getBRUTO());
        d[2] = standardizeReturn(resultCalcDataBase.getINITIAL_LIQUIDO());
        d[3] = standardizeReturn(resultCalcDataBase.getINITIAL_BRUTO());
        d[4] = standardizeReturn(resultCalcDataBase.getNEXT_LIQUIDO());
        d[5] = standardizeReturn(resultCalcDataBase.getNEXT_BRUTO());
        m.addRow(d);
        return m; 
    }
    
    public TableModel getResultTable() throws Exception 
    {
        DefaultTableModel m = new DefaultTableModel();
        String[] c = {"Tabela", "Liquido","Bruto", "Liquido Inicial", "Bruto Inicial", "Liquido Proximos anos", "Bruto Proximos anos"};
        m.setColumnIdentifiers(c);
        
        for (ResultCalcTable rTable : resultCalcDataBase.getTables()) 
        {
            Object[] d = new Object[7];
            d[0] = rTable.getName();
            d[1] = standardizeReturn(rTable.getLIQUIDO());
            d[2] = standardizeReturn(rTable.getBRUTO());
            d[3] = standardizeReturn(rTable.getINITIAL_LIQUIDO());
            d[4] = standardizeReturn(rTable.getINITIAL_BRUTO());
            d[5] = standardizeReturn(rTable.getNEXT_LIQUIDO());
            d[6] = standardizeReturn(rTable.getNEXT_BRUTO());
            m.addRow(d);
        }
        return m; 
    }
    
    private String standardizeReturn(double item)
    {
        return ((item/1024/1024) + " Mb");
    }
}